<?php
class html
{

    function __construct()
    {
		if(!defined('CREATEHTML')) define('CREATEHTML', 1);
    }

	function html()
	{
		$this->__construct();
	}

	function index()
	{
		ob_start();
        global $CATEGORY;
		include template('default', 'index');
		$file = APP_ROOT.'index.html';
		return createhtml($file);
	}

    function category( $catid, $page = 0 )
    {
        global $CATEGORY,$ishtml;
        $urlrules[0] = $CATEGORY[$catid]['url'] . 'index.html';//规则1
        $urlrules[1] = $CATEGORY[$catid]['url'] . $page . '.html';//规则2
		$urlrule = $page === 0 ? $urlrules[0] : $urlrules[1];//获取规则
        $siteid = $CATEGORY[$catid]['siteid'];
        if(empty($siteid)) return false;
		$file = site_root($siteid) . $urlrule;//创建地址
        //如果有子栏目
        if( $CATEGORY[$catid]['child'] == 1 )
		{
			$template = $CATEGORY[$catid]['c_temp'];
		}
		else
		{//没有子栏目
			if($page == 0)
            {
                $page = 1;
            }
			$template = $CATEGORY[$catid]['l_temp'];
		}
        $_SERVER['REQUEST_URI'] = $CATEGORY[$catid]['url'];
        $ishtml = true;
		ob_start();
		include template( 'default', $template );
        $ishtml = false;
		return createhtml( $file );
    }

    function show( $rid, $categoryid, $contentid, $is_update_related = 0 )
    {
        global $CATEGORY;
        if( !class_exists( 'content' ) )
		{
			require 'content.class.php';
		}
		$c = new content('category');
		extract( $GLOBALS, EXTR_SKIP );
		$r = $c->get_one( $contentid );
        $siteid = $r['siteid'];
        $rel_r = $c->get_cids($rid);
        if(!isset($CATEGORY[$categoryid])) return false;
        $template = $CATEGORY[$categoryid]['s_temp'];
		if( !$r )
        {
            return false;
        }
		if( $categoryid )
        {
            $catid = $categoryid;
        }
		if( $is_update_related )
		{
			$this->index();
			$pages = intval( 100 );
			$catids = explode( ',', $CATEGORY[$categoryid]['arrparentid'] );
			$catids[] = $categoryid;
			foreach( $catids as $cid )
			{
				if( $cid == 0 )
                {
                    continue;
                }
				for( $i = 0; $i <= $pages; $i++ )
				{
					if( $CATEGORY[$cid]['child']==1 && $i>0 )
                    {
                        continue;
                    }
					$this->category( $cid, $i );
				}
			}
		}
		if( $r['status'] != 99 )
        {
            return true;
        }
        extract( $r, EXTR_SKIP );//添加内容页直接读字段问题
		if( strpos( $r['content'], '[page]' ) !== false )
        {
            $contents = array_filter( explode( '[page]', $r['content'] ) );
            $pagenumber = count( $contents );
            for( $i=1; $i<=$pagenumber; $i++ )
            {
                $pageurls[$i] = date( 'Y',$r['addtime'] ) . "/" . date( 'md',$r['addtime'] ) . "/content/" . $rid . "_" . $i . ".html";
            }
            $page = $filesize = 0;
            foreach( $contents as $content )
            {
                $data['content'] = $content;
                $page++;
                $pages = $this->show_pages( $page, $pagenumber, $pageurls );
                if( $page == 1 )
                {
                    ob_start();
                    include template( 'default', $template );
                    $info = $rel_r['url'];
                    $file = APP_ROOT . $info;
                    createhtml( $file );
                }
                ob_start();
                include template( 'default', $template );
                $file = APP_ROOT . $pageurls[$page];
                $filesize += createhtml( $file );
            }
            $r['content'] = $contents[0];
            $pages = $this->show_pages(1, $pagenumber, $pageurls);
        }
		else
		{
			unset($pages);
			$info = $rel_r['url'];
			$file = site_root($siteid) . $info;
			ob_start();
			include template( 'default', $template );
			return createhtml( $file );
            exit;
		}
    }

    function show_pages( $curr_page, $num, $pageurls )
	{
		$multipage = '';
		$page = 11;
		$offset = 4;
		$pages = $num;
		$from = $curr_page - $offset;
		$to = $curr_page + $offset;
		$more = 0;
		if($page >= $pages)
		{
			$from = 2;
			$to = $pages-1;
		}
		else
		{
			if($from <= 1)
			{
				$to = $page-1;
				$from = 2;
			}
			elseif($to >= $pages)
			{
				$from = $pages-($page-2);
				$to = $pages-1;
			}
			$more = 1;
		}

		if($curr_page>0)
		{
			$perpage = $curr_page == 1 ? 1 : $curr_page-1;
			$multipage .= '<a href="'.$pageurls[$perpage].'">上一页</a>';
			if($curr_page==1)
			{
				$multipage .= '<u><b>1</b></u> ';
			}
			elseif($curr_page>6 && $more)
			{
				$multipage .= '<a href="'.$pageurls[1].'">1</a>..';
			}
			else
			{
				$multipage .= '<a href="'.$pageurls[1].'">1</a>';
			}
		}
		for($i = $from; $i <= $to; $i++)
		{
			if($i != $curr_page)
			{
				$multipage .= '<a href="'.$pageurls[$i].'">'.$i.'</a>';
			}
			else
			{
				$multipage .= ' <u><b>'.$i.'</b></u> ';
			}
		}
		if($curr_page<$pages)
		{
			if($curr_page<$pages-5 && $more)
			{
				$multipage .= '..<a href="'.$pageurls[$pages].'">'.$pages.'</a> <a href="'.$pageurls[$curr_page+1].'">下一页</a>';
			}
			else
			{
				$multipage .= '<a href="'.$pageurls[$pages].'">'.$pages.'</a> <a href="'.$pageurls[$curr_page+1].'">下一页</a>';
			}
		}
		elseif($curr_page==$pages)
		{
			$multipage .= ' <u><b>'.$pages.'</b></u><a href="'.$pageurls[$curr_page].'">下一页</a>';
		}
		return $multipage;
	}
}
?>
